﻿using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.ComponentModel;

namespace PMAF.K3Cloud.SCM.Bill.BusinessPlugIn
{
    [HotUpdate]
    [Description("销售订单流水号过滤")]
    public class SaleOrder : AbstractBillPlugIn
    {
        /// <summary>
        /// 值更新事件
        /// </summary>
        /// <param name="e"></param>
        public override void DataChanged(DataChangedEventArgs e)
        {
            base.DataChanged(e);
            if (e.Field.Key == "FQty")
            {
                decimal newValue = Convert.ToDecimal(e.NewValue);
                decimal oldValue = Convert.ToDecimal(e.OldValue);
                //View.ShowMessage("数量改变:" + (oldValue-newValue));
            }
        }

        /// <summary>
        /// F7之前事件
        /// </summary>
        /// <param name="e"></param>
        public override void BeforeF7Select(BeforeF7SelectEventArgs e)
        {
            base.BeforeF7Select(e);
            if (e.FieldKey.Equals("F_PMAF_PurchaseSN"))
            {
                DynamicObject saleOrg = (DynamicObject)View.Model.GetValue("FSaleOrgId");
                int saleOrgId = Convert.ToInt32(saleOrg["Id"]);
                
                //获取当前索引行
                int rowindex = View.Model.GetEntryCurrentRowIndex("FSaleOrderEntry");
                //当前行对应的物料编码
                DynamicObject material = (DynamicObject)View.Model.GetValue("FMaterialId", rowindex);
                /*
                List<SelectorItemInfo> materialFields = new List<SelectorItemInfo>
                {
                    new SelectorItemInfo("Id"),
                    new SelectorItemInfo("FNumber")
                };
                OQLFilter materialFilter = new OQLFilter
                {
                    new OQLFilterHeadEntityItem() { FilterString = string.Format("FNumber='{0}'", material["Number"]) }
                };
                DynamicObject[] materials = BusinessDataServiceHelper.Load(Context, "BD_MATERIAL", materialFields, materialFilter);
                string ids = "0";
                foreach(DynamicObject materialResult in materials)
                {
                    ids = ids + "," + materialResult["Id"];
                }*/
                if (material == null)
                {
                    View.ShowWarnningMessage("请先选择物料信息");
                    e.Cancel = true;
                }
                else
                {
                    //过滤物料为当前行的流水号
                    e.ListFilterParameter.Filter = string.Format("F_PMAF_PurOrg = {0} AND F_PMAF_CURRENTQTY > 0", saleOrgId);
                }
            }
        }
    }
}
